package com.tianzmp.ttq.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.tianzmp.ttq.admin.controller.reqDto.MenuAddReqDto;
import com.tianzmp.ttq.admin.controller.reqDto.MenuPageListReqDto;
import com.tianzmp.ttq.admin.controller.reqDto.MenuUpdateReqDto;
import com.tianzmp.ttq.admin.controller.resDto.MenuListResDto;
import com.tianzmp.ttq.admin.controller.resDto.MenuTreeResDto;
import com.tianzmp.ttq.admin.entity.Menu;
import com.tianzmp.ttq.admin.entity.PageInfo;
import com.tianzmp.ttq.admin.mapper.MenuMapper;
import com.tianzmp.ttq.admin.service.IMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单 服务实现类
 * </p>
 *
 * @author ttq
 * @since 2022-07-30
 */
@Service
public class MenuServiceImpl implements IMenuService {

    @Resource
    private MenuMapper menuDao;


    @Override
    public List<MenuListResDto> list() {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<Menu>().in(Menu::getType, Lists.newArrayList(1, 2));
        List<Menu> menus = menuDao.selectList(queryWrapper);
        return menus.stream().map(ex -> {
            MenuListResDto menuListResDto = new MenuListResDto();
            BeanUtils.copyProperties(ex, menuListResDto);
            return menuListResDto;
        }).collect(Collectors.toList());
    }

    @Override
    public PageInfo<List<MenuTreeResDto>> pageList(MenuPageListReqDto dto) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<Menu>()
                .like(StringUtils.isNotBlank(dto.getMenuName()), Menu::getMenuName, dto.getMenuName());
        List<Menu> result = menuDao.selectList(queryWrapper);

        long count = result.stream().filter(ex -> ex.getParentId().equals(0L)).count();
        int start = dto.getCurrent() <= 1 ? 0 : (dto.getCurrent() - 1) * dto.getSize();
        List<MenuTreeResDto> collect = result.stream().filter(o->o.getParentId().equals(0L)).map(ex -> {
            MenuTreeResDto resDto = new MenuTreeResDto();
            BeanUtils.copyProperties(ex, resDto);
            resDto.setChildren(getChild(ex.getMenuId(),result));
            return resDto;
        }).skip(start).limit(dto.getSize()).collect(Collectors.toList());
        return new PageInfo(dto.getCurrent(),dto.getSize(), count, collect);
    }


    private List<MenuTreeResDto> getChild(Long id, List<Menu> list){
        return list.stream().filter(o -> o.getParentId().equals(id)).map(o -> {
            MenuTreeResDto menuTree = new MenuTreeResDto();
            BeanUtils.copyProperties(o, menuTree);
            menuTree.setChildren(getChild(o.getMenuId(), list));
            return menuTree;
        }).collect(Collectors.toList());
    }

    /**
    * <pre>
     *     新增
    * </pre>
    * @param dto
    * @return boolean
    * @author zhump
    * @date 2023/12/7 9:53
    */
    @Override
    public boolean insert(MenuAddReqDto dto) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(dto,menu);
        return menuDao.insert(menu) > 0;
    }

    @Override
    public boolean update(MenuUpdateReqDto menu) {
        Menu dbMenu = new Menu();
        BeanUtils.copyProperties(menu,dbMenu);
        return menuDao.updateById(dbMenu) > 0;
    }

    @Override
    public Menu getById(Long id) {
        return menuDao.selectById(id);
    }

    @Override
    public boolean delete(Long userId) {
        return menuDao.deleteById(userId) > 0;
    }

    @Override
    public boolean userAuthorize(Long userId, List<Long> roleIds) {
        return false;
    }
}
